package com.esisa.business;

import java.util.List;

import com.esisa.dao.BetailDao;
import com.esisa.dao.CompteDao;
import com.esisa.dao.FermierDao;
import com.esisa.exceptions.UserAlreadyExistsException;
import com.esisa.models.Betail;
import com.esisa.models.Fermier;

public class DefaultFermierManager implements FermierManager {

	private FermierDao fermierDao;
	private CompteDao compteDao;
	private BetailDao betailDao;
	@Override
	public boolean create(Fermier fermier) {
		try {
			fermierDao.insert(fermier);
			compteDao.insert(fermier.getCompte());
		} catch (UserAlreadyExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public void setFermierDao(FermierDao fermierDao) {
		this.fermierDao = fermierDao;
		
	}
	public void setCompteDao(CompteDao compteDao) {
		this.compteDao = compteDao;
	}
	@Override
	public List<Fermier> findAll() {
		return fermierDao.selectAll();
	}
	@Override
	public Fermier find(Long id) {
		return fermierDao.select(id);
	}
	@Override
	public void delete(Fermier fermier) {
			compteDao.delete(fermier.getCompte());
			fermierDao.delete(fermier);
	}
	@Override
	public void update(Fermier fermier) {
		fermierDao.update(fermier);
		
	}
	// betails  fermer //
	
	@Override
	public boolean createBetail(Betail betail) {
		
		System.out.println("Def fermier maanag " + betail.toString());
		return this.betailDao.insert(betail);
	}
	
	public BetailDao getBetailDao() {
		return betailDao;
	}
	public void setBetailDao(BetailDao betailDao) {
		this.betailDao = betailDao;
	}
	
	
	
	@Override
	public List<Betail> selectAllBetails() {
		return betailDao.selectAllBetails();
		
	}
	@Override
	public List<Betail> selectAllBetails(int page, int pas) {
		
		int from=(page-1)*pas;
		List<Betail> res=betailDao.selectAllBetails(from,pas);
		return res;
	}
	@Override
	public int getCountBetails() {
		
		return betailDao.getCountBetails();
	}
	@Override
	public Betail findBetail(Long id) {
		return betailDao.select(id);
	}
	
	

}
